package com.queue;


public class LinkedQueue<E> implements Queue<E>{
    private class Node<E>{
        public E node;
        public Node next;


        public Node(E node, Node next) {
            this.node = node;
            this.next = next;
        }
        public Node(E node){
            this.node = node;
            this.next = null;
        }


        @Override
        public String toString() {
            return "Node{" + "node=" + node + ", next=" + next + '}';
        }
    }

    private Node<E> head, tail;
    private int size;

    public LinkedQueue() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void enQueue(E e) {
        //队列中没有元素
        if(tail == null){
            tail = new Node<>(e);
            head = tail;
        }else{
            tail.next = new Node<>(e);
            tail = tail.next;
        }
        size++;
    }

    @Override
    public E deQueue() {
        if(isEmpty()){
            throw new IllegalArgumentException("queue is empty");
        }
        Node<E> retNode = head;
        head = head.next;
        if(head == null){
            tail = head;
        }
        size--;
        return retNode.node;
    }

    @Override
    public E getFront() {
        return head.node;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("LinkedQueue{");
        Node<E> cur = head;
        while (cur != null){
            sb.append(cur.node);
            sb.append(",");
            cur = cur.next;
        }
        sb.append("}队列尾");
        return sb.toString();
    }
}
